/* -*-mode:java; c-basic-offset:2; -*- */
/* JSchSession
 * Copyright (C) 2002,2007 ymnk, JCraft,Inc.
 *  
 * Written by: ymnk<ymnk@jcaft.com>
 *   
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package net.sshiffon.jctermish;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Proxy;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class SshSessionManager {
  private JSch jsch= new JSch();

  private Session session=null;

  private SshSessionManager(){
  }
    
  public static SshSessionManager getSession(String username, String password,
      String hostname, int port, UserInfo userinfo, Proxy proxy, String knownHostsFile)
      throws JSchException{
    //String key=getPoolKey(id, username, hostname, port);
      SshSessionManager jschSession= new SshSessionManager();
      jschSession.jsch.setKnownHosts(knownHostsFile);
        try{
          jschSession.createSession(username, password, hostname, port, userinfo,
              proxy);
        }
        catch(JSchException e){
          if(isAuthenticationFailure(e)){
            jschSession.createSession(username, password, hostname, port, userinfo,
                proxy);
          }
          else{
            throw e;
          }
        }

        if(jschSession.getSession()==null)
          throw new JSchException("The JSch service is not available");

      return jschSession;
  }

  public JSch getJSch(){
    return jsch;
  }

  private Session createSession(String username, String password,
      String hostname, int port, UserInfo userinfo, Proxy proxy)
      throws JSchException{
    session=getJSch().getSession(username, hostname, port);

    session.setTimeout(60000);
    if(password!=null)
      session.setPassword(password);
    session.setUserInfo(userinfo);
    if(proxy!=null)
      session.setProxy(proxy);
    session.connect(60000);
    session.setServerAliveInterval(60000);
    return session;
  }


  public Session getSession(){
    return session;
  }

  public void dispose(){
    if(session.isConnected()){
      session.disconnect();
    }
  }

  public static boolean isAuthenticationFailure(JSchException ee){
    return ee.getMessage().equals("Auth fail");
  }
  public static boolean isCancel(JSchException ee){
    return ee.getMessage().equals("Auth cancel");
  }

}
